package com.seedeer.framework.config;

import com.seedeer.common.support.convert.UniversalEnumConverterFactory;
import com.seedeer.framework.web.interceptor.RevisionInterceptor;
import com.seedeer.pms.filter.PmsClientFilter;
import com.seedeer.project.service.system.ISysParamService;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import java.util.Locale;

/**
 * MVC基础配置
 *
 * @author wangqiang
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    public MvcConfig(Environment environment, ISysParamService sysParamService) {
        this.environment = environment;
        this.sysParamService = sysParamService;
    }

    private final Environment environment;

    private final ISysParamService sysParamService;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //版本号拦截器
        registry.addInterceptor(new RevisionInterceptor(sysParamService)).addPathPatterns("/api/**");
    }

    /**
     * 打印请求相关日志信息
     */
    @Bean
    public CommonsRequestLoggingFilter logFilter() {
        CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
        //是否打印请求参数
        filter.setIncludeQueryString(true);
        //是否打印client、session、user
        filter.setIncludeClientInfo(false);
        //是否打印headers
        filter.setIncludeHeaders(false);
        //先判断request的content的长度，如果超过设置maxPayload的长度，则按照maxPayload进行截取，如果出现异常，则payload=[unknown]
        filter.setIncludePayload(true);
        filter.setMaxPayloadLength(10000);
        filter.setAfterMessagePrefix("REQUEST : ");
        //指明该bean运行环境
        filter.setEnvironment(environment);
        return filter;
    }

    /**
     * 多语言使用cookie
     *
     * @return org.springframework.web.servlet.LocaleResolver
     * @author wangqiang
     * @date 2020/4/17 14:52
     */
    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver localeResolver = new CookieLocaleResolver();
        //默认语种
        localeResolver.setDefaultLocale(Locale.US);
        //cookie名称
        localeResolver.setCookieName("language");
        return localeResolver;
    }

    /**
     * Add {@link org.springframework.core.convert.converter.Converter Converters}
     * and {@link org.springframework.format.Formatter Formatters} in addition to the ones
     * registered by default.
     *
     * @param registry 注册器
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 添加枚举全局转换工厂
        registry.addConverterFactory(new UniversalEnumConverterFactory());
    }



    /**
     * 权限
     * @return
     */
    @Bean
    public FilterRegistrationBean<PmsClientFilter> registration() {
        FilterRegistrationBean<PmsClientFilter> registration = new FilterRegistrationBean<>();

        registration.setName("Pms Client Filter");
        registration.setFilter(new PmsClientFilter());
        registration.addUrlPatterns("/api/**");

        return registration;
    }

}
